﻿using UnityEngine;
using System.Collections;

[AddComponentMenu("MyGame/SuperEnemy")]
public class SuperEnemy : Enemy {

    public Transform m_rocket;          //用于标识子弹
    protected float m_fireTimer = 2;    //用来控制发射子弹的时间间隔
    protected Transform m_player;       //指向主角的飞船
    //声音
    public AudioClip m_shootClip;

    //声音源
    protected AudioSource m_audio;

    //Awake方法先于Start方法
    void Awake()
    {
        //用于得到飞船的实体
        GameObject obj = GameObject.FindGameObjectWithTag("Player");
        if(obj!=null){
            m_player = obj.transform;
        }
    }

    void Start()
    {
        m_transform = this.transform;
        m_audio = this.audio;
    }

    protected override void UpdateMove()
    {
        //下面的方法用于发射子弹
        m_fireTimer -= Time.fixedDeltaTime;
        if(m_fireTimer<=0){
            m_fireTimer = 2;
            if(m_player!=null){
                Vector3 relativePos = m_transform.position - m_player.position;
                m_audio.PlayOneShot(m_shootClip);
               Instantiate(m_rocket, m_transform.position, Quaternion.LookRotation(relativePos));
            }
        }

        //前进
        m_transform.Translate(new Vector3(0, 0, -m_speed * Time.deltaTime));
    }

    //检测碰撞
    void OnTriggerEnter(Collider other)
    {
        if (other.tag.CompareTo("PlayerRocket") == 0)
        {
            Rocket rocket = other.GetComponent<Rocket>();
            if (rocket != null)
            {
                GameManager.Instance.AddScore(m_point);
                m_life -= rocket.m_power;
                if (m_life <= 0)
                {
                    Instantiate(m_explosionFX, m_transform.position, m_transform.rotation);
                    Destroy(this.gameObject);
                }
            }
            else if (other.tag.CompareTo("Player") == 0)
            {
                m_life = 0;
                Instantiate(m_explosionFX, m_transform.position, m_transform.rotation);
                Destroy(this.gameObject);
            }
        }
    }
}
